package com.snail.billing.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpConst;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.BillingBroadcast;
import com.snail.billing.BillingBroadcastImpl;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.AccountManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.session.UpdateAliasSession;
import com.snail.billing.util.BillingUtil;

public class NicknameFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputNickname;
	private EditText inputPwd;
	private EditText inputPwd2;
	private View button;

	private String account;
	private String pwd;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_nickname_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_user_center_text_nickname));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputNickname = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_nickname_bind_input_nickname));
		inputPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_nickname_bind_input_pwd));
		inputPwd2 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_nickname_bind_input_pwd2));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_nickname_bind_button));
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button)) {
			account = inputNickname.getText().toString();
			if (TextUtils.isEmpty(account)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_nickname_input_nickname));
				return;
			}
			pwd = inputPwd.getText().toString();
			if (TextUtils.isEmpty(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_nickname_input_pwd));
				return;
			}
			String pwd2 = inputPwd2.getText().toString();
			if (TextUtils.isEmpty(pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_nickname_input_pwd2));
				return;
			}
			if (!TextUtils.equals(pwd, pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_not_same));
				return;
			}
			if (pwd.length() < 6) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_must_more_than_six));
				return;
			}
			if (!BillingUtil.validatePwd(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_validate));
				return;
			}
			requestUpdateAlias(account, pwd);
		}
	}

	private void requestUpdateAlias(String account, String pwd) {
		HttpApp httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
		UpdateAliasSession updateAliasSession = new UpdateAliasSession(account, pwd);
		httpApp.request(updateAliasSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
			if (jsonBase.getCode() == 1) {
				DataCache.getInstance().importParams.account = account;
				AccountManager.saveBindAccount(account, pwd);
				AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
				builder.setMessage(ResUtil.getString(Resource.string.snailbilling_nickname_bind_ok));
				builder.setPositiveButton(ResUtil.getString(HttpConst.string.NET_OK), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						String action = BillingBroadcast.ACTION_BIND_ACCOUNT_SUCCEED;
						BillingBroadcast broadcast = BillingBroadcastImpl.getInstance();
						broadcast.contains(action);
						if (broadcast.getResult() != null) {
							broadcast.getResult().onResult(1, action, new String[] { account, pwd });
						}
						getPageManager().backward();
					}
				});
				builder.show();
			} else {
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
			}
		}
	}

}
